﻿using SQLite;
using System;
using System.Collections.Generic;

namespace SimpleTerminal.Common
{
    public class Datastore : IDisposable
    {
        private readonly SQLiteConnection connection;

        public SQLiteConnection Connection => connection;

        public Datastore()
        {
            SQLiteConnectionString connectionString = new SQLiteConnectionString("./datastore.db");
            connection = new SQLiteConnection(connectionString);

        }

        public void Open()
        {

        }
        public List<Keyword> Keywords()
        {
            return Keywords(false);
        }
        public List<Keyword> Keywords(bool all)
        {
            List<Keyword> keywords = Connection.Query<Keyword>("select * from keyword" + (all ? "":" where enable = 1") + " order by sortby asc");
            return keywords;
        }
        public Keyword Keyword(int id)
        {
            //"select * from keyword where id = " + 
            Keyword keyword = Connection.Find<Keyword>(id);
            return keyword;
        }
        public bool UpdateKeyword(Keyword keyword)
        {
            return Connection.Update(keyword) > 0;
        }

        public bool InsetKeyword(Keyword keyword)
        {
            return Connection.Insert(keyword) > 0;
        }

        public bool RemoveKeyword(int id)
        {
            return Connection.Delete<Keyword>(id) > 0;
        }

        public void Dispose()
        {
            ((IDisposable)Connection).Dispose();
        }
    }
}
